package com.code.google.mdsc.model.impl;

import java.math.BigDecimal;
import java.util.GregorianCalendar;

import com.code.google.mdsc.model.Debtor;

/**
 *
 */
public class DebtorImpl implements Debtor {

	private String bic;
	private String iban;
	private String mandate;
	private String accountHolder;
	private BigDecimal bookingVolumne;
	private GregorianCalendar mandatorySignatureDate;
	private String unstructuredPurpose;
	private String sequenceType;

	public DebtorImpl(String bic, String iban, String mandatoryNumber,
			String accountHolder, BigDecimal bookingVolumne,
			GregorianCalendar mandatorySignatureDate,
			String unstructuredPurpose, String sequenceType) {
		super();
		this.setBIC(bic);
		this.setIBAN(iban);
		this.setMandate(mandatoryNumber);
		this.setAccountHolder(accountHolder);
		this.setBookingVolume(bookingVolumne);
		this.setMandateSignatureDate(mandatorySignatureDate);
		this.setUnstructuredPurpose(unstructuredPurpose);
		this.setSequenceType(sequenceType);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		DebtorImpl other = (DebtorImpl) obj;
		if (accountHolder == null) {
			if (other.accountHolder != null)
				return false;
		} else if (!accountHolder.equals(other.accountHolder))
			return false;
		if (bic == null) {
			if (other.bic != null)
				return false;
		} else if (!bic.equals(other.bic))
			return false;
		if (bookingVolumne == null) {
			if (other.bookingVolumne != null)
				return false;
		} else if (!bookingVolumne.equals(other.bookingVolumne))
			return false;
		if (iban == null) {
			if (other.iban != null)
				return false;
		} else if (!iban.equals(other.iban))
			return false;
		if (mandate == null) {
			if (other.mandate != null)
				return false;
		} else if (!mandate.equals(other.mandate))
			return false;
		if (mandatorySignatureDate == null) {
			if (other.mandatorySignatureDate != null)
				return false;
		} else if (!mandatorySignatureDate.equals(other.mandatorySignatureDate))
			return false;
		if (sequenceType != other.sequenceType)
			return false;
		if (unstructuredPurpose == null) {
			if (other.unstructuredPurpose != null)
				return false;
		} else if (!unstructuredPurpose.equals(other.unstructuredPurpose))
			return false;
		return true;
	}

	@Override
	public String getAccountHolder() {
		return accountHolder;
	}

	@Override
	public String getBIC() {
		return bic;
	}

	@Override
	public BigDecimal getBookingVolume() {
		return bookingVolumne;
	}

	@Override
	public String getIBAN() {
		return iban;
	}

	@Override
	public String getMandate() {
		return mandate;
	}

	@Override
	public GregorianCalendar getMandateSignatureDate() {
		return mandatorySignatureDate;
	}

	@Override
	public String getSequenceType() {
		return sequenceType;
	}

	@Override
	public String getUnstructuredPurpose() {
		return unstructuredPurpose;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((accountHolder == null) ? 0 : accountHolder.hashCode());
		result = prime * result + ((bic == null) ? 0 : bic.hashCode());
		result = prime * result
				+ ((bookingVolumne == null) ? 0 : bookingVolumne.hashCode());
		result = prime * result + ((iban == null) ? 0 : iban.hashCode());
		result = prime * result + ((mandate == null) ? 0 : mandate.hashCode());
		result = prime
				* result
				+ ((mandatorySignatureDate == null) ? 0
						: mandatorySignatureDate.hashCode());
		result = prime * result
				+ ((sequenceType == null) ? 0 : sequenceType.hashCode());
		result = prime
				* result
				+ ((unstructuredPurpose == null) ? 0 : unstructuredPurpose
						.hashCode());
		return result;
	}

	@Override
	public void setAccountHolder(String accountHolder) {
		if (accountHolder == null || accountHolder.trim().isEmpty())
			throw new IllegalArgumentException(
					"paramter must not be null or an empty string");
		this.accountHolder = accountHolder;
	}

	@Override
	public void setBIC(String bic) {
		if (bic == null || bic.trim().isEmpty())
			throw new IllegalArgumentException(
					"paramter must not be null or an empty string");
		this.bic = bic;
	}

	@Override
	public void setBookingVolume(BigDecimal bookingVolumne) {
		if (bookingVolumne == null || bookingVolumne.floatValue() <= 0.0)
			throw new IllegalArgumentException(
					"paramter must be greater than zero");
		this.bookingVolumne = bookingVolumne;
	}

	@Override
	public void setIBAN(String iban) {
		if (iban == null || iban.trim().isEmpty())
			throw new IllegalArgumentException(
					"paramter must not be null or an empty string");
		this.iban = iban;
	}

	@Override
	public void setMandate(String mandate) {
		if (mandate == null || mandate.trim().isEmpty())
			throw new IllegalArgumentException(
					"paramter must not be null or an empty string");
		this.mandate = mandate;
	}

	@Override
	public void setMandateSignatureDate(GregorianCalendar mandatorySignatureDate) {
		if (mandatorySignatureDate == null)
			throw new IllegalArgumentException("parameter must not be null");
		this.mandatorySignatureDate = mandatorySignatureDate;
	}

	@Override
	public void setSequenceType(String sequenceType) {
		if (sequenceType == null)
			throw new IllegalArgumentException("parameter must not be null");
		// a bit dirty here...
		switch (sequenceType) {
		case "FRST":
		case "RCUR":
		case "FNAL":
		case "OOFF":
			this.sequenceType = sequenceType;
			break;
		default:
			throw new IllegalArgumentException(
					"SequenceType must be FRST, RCUR, FNAL or OOFF.");
		}
	}

	@Override
	public void setUnstructuredPurpose(String unstructuredPurpose) {
		if (unstructuredPurpose == null || unstructuredPurpose.trim().isEmpty()
				|| unstructuredPurpose.length() > 140)
			throw new IllegalArgumentException(
					"paramter must not be null, an empty string or longer than 140 characters");

		this.unstructuredPurpose = unstructuredPurpose;
	}

}
